<template>
    <div class="homePage">
        <h1>{{ t('Home.Text1') }}</h1>
        <p>{{ t('Home.Text2') }}</p>
        <div class="upload-section flex" @click="triggerFileInput">
            <div class="upload-box flex">
                <img src="@/assets/home/upload.png" alt="" />
                <span>{{ t('Home.UploadText') }}</span>
                <!-- <p>{{ t('Home.UploadTip') }}</p> -->
            </div>
            <!-- <div class="upload-buttons">
                <button>
                    <img src="@/assets/home/link.png" alt="" />
                    {{ t('Home.BtnText1') }}
                </button>
                <button @click="triggerFileInput">
                    <img src="@/assets/home/file.png" alt="" />
                    {{ t('Home.BtnText2') }}
                </button>
            </div> -->
        </div>
        <div class="suggestions">
            <p>{{ t('Home.NoPreparation') }}</p>
            <div class="image-thumbnails">
                <!-- Add image thumbnails here -->
                <img src="@/assets/home/thumb-1.jpg" alt="Image 1" />
                <img src="@/assets/home/thumb-2.jpg" alt="Image 2" />
                <img src="@/assets/home/thumb-3.jpg" alt="Image 3" />
                <img src="@/assets/home/thumb-4.jpg" alt="Image 4" />
            </div>
        </div>

        <div class="otherBlock">
            <h1>{{ t('Home.PowerfulAI') }}</h1>
            <p>{{ t('Home.ProfessionalTech') }}</p>

            <div class="features">
                <div class="feature-card">
                    <div class="imgBox">
                        <img src="@/assets/home/shandian.png" alt="快速处理" />
                    </div>
                    <h3>{{ t('Home.FastProcess') }}</h3>
                    <p>{{ t('Home.FastProcessDesc') }}</p>
                </div>
                <div class="feature-card">
                    <div class="imgBox">
                        <img src="@/assets/home/anquan.png" alt="隐私安全" />
                    </div>
                    <h3>{{ t('Home.PrivacySecurity') }}</h3>
                    <p>{{ t('Home.PrivacySecurityDesc') }}</p>
                </div>
                <div class="feature-card">
                    <div class="imgBox">
                        <img src="@/assets/home/huojian.png" alt="高清输出" />
                    </div>
                    <h3>{{ t('Home.HDOutput') }}</h3>
                    <p>{{ t('Home.HDOutputDesc') }}</p>
                </div>
            </div>

            <div style="background-color: #ffe3d3; padding: 40px 20px 20px; box-sizing: border-box; border-radius: 8px">
                <h2>{{ t('Home.UserReviews') }}</h2>
                <p>{{ t('Home.GlobalExperience') }}</p>
                <div class="testimonials">
                    <div class="testimonial-card">
                        <div class="topInfo flex">
                            <img src="@/assets/header/user.png" :alt="t('Home.Reviews.User1Alt')" />
                            <h4>{{ t('Home.Reviews.User1Name') }}</h4>
                        </div>
                        <p>{{ t('Home.Reviews.User1Text') }}</p>
                    </div>
                    <div class="testimonial-card">
                        <div class="topInfo flex">
                            <img src="@/assets/header/user.png" :alt="t('Home.Reviews.User2Alt')" />
                            <h4>{{ t('Home.Reviews.User2Name') }}</h4>
                        </div>
                        <p>{{ t('Home.Reviews.User2Text') }}</p>
                    </div>
                    <div class="testimonial-card">
                        <div class="topInfo flex">
                            <img src="@/assets/header/user.png" :alt="t('Home.Reviews.User3Alt')" />
                            <h4>{{ t('Home.Reviews.User3Name') }}</h4>
                        </div>
                        <p>{{ t('Home.Reviews.User3Text') }}</p>
                    </div>
                </div>
            </div>

            <div class="faq-section">
                <h2>{{ t('Home.FAQ') }}</h2>
                <p>{{ t('Home.FAQDesc') }}</p>
                <div class="faq">
                    <div class="faq-item" v-for="(item, index) in faqs" :key="index">
                        <div class="faq-question" @click="toggleFaq(index)">
                            <span>{{ item.question }}</span>
                            <span>{{ activeFaq === index ? '−' : '+' }}</span>
                        </div>
                        <div v-if="activeFaq === index" class="faq-answer">
                            <p>{{ item.answer }}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input type="file" ref="fileInput" accept="image/*" style="display: none" @change="handleFileSelect" />
    <Footer />
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import Footer from '@/layout/Footer.vue'
import { useI18n } from 'vue-i18n'
import { Files } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/userStore'
const { t, locale } = useI18n()
const files = ref([])
const fileInput = ref()
const router = useRouter()
const userStore = useUserStore()
// 将 faqs 改为 computed 属性
const faqs = computed(() => [
    {
        question: t('Home.FAQs.Q1'),
        answer: t('Home.FAQs.A1')
    },
    {
        question: t('Home.FAQs.Q2'),
        answer: t('Home.FAQs.A2')
    },
    {
        question: t('Home.FAQs.Q3'),
        answer: t('Home.FAQs.A3')
    },
    {
        question: t('Home.FAQs.Q4'),
        answer: t('Home.FAQs.A4')
    }
])

const activeFaq = ref(null)

const toggleFaq = index => {
    activeFaq.value = activeFaq.value === index ? null : index
}

const triggerFileInput = () => {
    fileInput.value.click()
}

const handleFileSelect = async event => {
    const selectedFiles = Array.from(event.target.files)
    for (const file of selectedFiles) {
        const dimensions = await getImageDimensions(file)
        files.value.push({
            file,
            name: file.name,
            preview: URL.createObjectURL(file),
            dimensions: `${dimensions.width}x${dimensions.height}px`
        })
    }
    event.target.value = '' // Reset input
    setTimeout(() => {
        userStore.setFile(files.value[0])
        router.push('/upload')
    }, 100)
}

const getImageDimensions = file => {
    return new Promise(resolve => {
        const img = new Image()
        img.onload = () => {
            resolve({ width: img.width, height: img.height })
        }
        img.src = URL.createObjectURL(file)
    })
}

</script>

<style lang="scss" scoped>
.homePage {
    color: #333;
    text-align: center;
    padding: 20px;
    padding-top: 50px;
    box-sizing: border-box;
    height: auto;
    padding-bottom: 100px;

    h1 {
        font-size: 52px;
        margin-bottom: 20px;
    }

    p {
        margin-bottom: 20px;
        font-size: 24px;
    }

    .upload-section {
        width: 800px;
        //height: 360px;
        border: 1px dashed #767676;
        padding: 20px;
        margin-bottom: 20px;
        margin: 0 auto;
        background: #fff;
        flex-direction: column;
        border-radius: 10px;
        justify-content: flex-start;
        margin-bottom: 40px;

        .upload-box {
            background-color: #efefef;
            // margin-bottom: 40px;
            font-size: 24px;
            width: 100%;
            padding: 60px 0;
            flex-direction: column;
            cursor: pointer;

            span {
                font-size: 24px;
                font-weight: 600;
            }

            p {
                margin-top: 10px;
            }

            img {
                width: 60px;
                height: 60px;
                margin-bottom: 20px;
            }
        }

        .upload-buttons {
            display: flex;
            justify-content: center;
            gap: 20px;

            button {
                margin: 0 5px;
                padding: 10px 20px;
                background-color: #f0f0f0;
                border: none;
                cursor: pointer;
                font-size: 20px;
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 10px;

                img {
                    width: 20px;
                    height: 20px;
                }
            }
        }
    }

    .suggestions {
        .image-thumbnails {
            display: flex;
            justify-content: center;
            gap: 10px;

            img {
                width: 100px;
                height: auto;
                cursor: pointer;
            }
        }
    }

    .otherBlock {
        width: 80%;
        margin: 0 auto;
        margin-top: 150px;
        h1,
        h2 {
            font-size: 32px;
            margin-bottom: 20px;
        }

        p {
            margin-bottom: 40px;
            font-size: 18px;
        }

        .features {
            display: flex;
            justify-content: space-around;
            margin-bottom: 60px;

            .feature-card {
                background: #f3d7d7;
                padding: 20px;
                border-radius: 10px;
                width: 30%;
                text-align: left;

                .imgBox {
                    padding: 12px;
                    width: fit-content;
                    background-color: #e97a35;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-bottom: 10px;
                }

                img {
                    width: 30px;
                    height: 30px;
                }

                h3 {
                    font-size: 20px;
                    margin-bottom: 10px;
                }

                p {
                    font-size: 16px;
                }
            }
        }

        .testimonials {
            display: flex;
            justify-content: space-around;

            .testimonial-card {
                background: #fff;
                padding: 20px;
                border-radius: 10px;
                width: 30%;
                text-align: left;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

                .topInfo {
                    justify-content: flex-start;
                    gap: 20px;
                }

                img {
                    width: 50px;
                    height: 50px;
                    border-radius: 50%;
                    margin-bottom: 10px;
                }

                h4 {
                    font-size: 18px;
                    margin-bottom: 10px;
                }

                p {
                    font-size: 16px;
                }
            }
        }
    }

    .faq-section {
        margin-top: 50px;
        text-align: center;

        h2 {
            font-size: 32px;
            margin-bottom: 10px;
        }

        p {
            margin-bottom: 20px;
            font-size: 18px;
        }

        .faq {
            width: 80%;
            margin: 0 auto;
            text-align: left;

            .faq-item {
                background: #f9f9f9;
                border-radius: 8px;
                margin-bottom: 10px;
                overflow: hidden;
                box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);

                .faq-question {
                    padding: 15px;
                    font-size: 18px;
                    display: flex;
                    justify-content: space-between;
                    cursor: pointer;
                    background: #fff;
                }

                .faq-answer {
                    padding: 15px;
                    background: #f1f1f1;
                    font-size: 16px;
                }
            }
        }
    }
}
</style>
